package m5;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Cousumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory f = new ConnectionFactory();
        f.setPort(5672);
        f.setHost("192.168.64.140");
        f.setUsername("admin");
        f.setPassword("admin");
        //f.setVirtualHost(); 使用自己的空间使用
        Channel con = f.newConnection().createChannel();
    //    String queue = UUID.randomUUID().toString();/*随机队列*/
                                        /*持久化  独占  自动删除  */
        /*让服务器自动命名,自动提供参数 随机名，false，true，true，null*/
        String queue = con.queueDeclare().getQueue();
        con.exchangeDeclare("topic_log",BuiltinExchangeType.TOPIC);
        System.out.println("输入绑定建 ： 用括号隔开");
        String s = new Scanner(System.in).nextLine();
        String[] a = s.split("\\s+");/*\s空白字符 + 指1~多个字符*/
        for(String k : a ){
            con.queueBind(queue,"topic_log",k);
        }
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                byte[] body = delivery.getBody();
                String s1 = new String(body);
                System.out.println(s1);
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };
        String jg = con.basicConsume(queue,true,deliverCallback,cancelCallback);
    }
}
